<%@ include file="../import_page.jsp"%>
<%
	//login checking
	if(!loginedChecking(session, UsersType.staff)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
		return;
	}
	
	//select unit check
	if(!selectUnitCheck(session)) {
		//if user didn't select an Unit
		//redirtect to select page
		response.sendRedirect(URL_SELECT_UNIT);
		return;
	}
	
	//input parameter
	int acId = -1;
	String acIdStr = request.getParameter(PAR_ASSIGNMENT_ID);
	if(isEmptyString(acIdStr)) {
		response.sendRedirect(URL_VIEW_ASSIGNMENT);
		return;
	}
	//Convert string to int
	try {
		acId = Integer.parseInt(acIdStr);
		if(acId < 1)
			throw new Exception();
	} catch(Exception err) {
		//incorrect input, go back view assignment
		response.sendRedirect(URL_VIEW_ASSIGNMENT);
		return;
	}
	
	//input parameter
	Users submitBy = null;
	String submitByStr = request.getParameter(PAR_SUBMIT_BY);
	if(isEmptyString(submitByStr) || submitByStr.length() > Users.ID_MAX_LENGTH) {
		response.sendRedirect(URL_VIEW_ASSIGNMENT);
		return;
	}
	//get User info of submit By
	try {
		UserControl uCon = new UserControl();
		submitBy = uCon.getUser(submitByStr);
		uCon.closeDBConnection();
	} catch(Exception err) {
		//incorrect input, go back view assignment
		response.sendRedirect(URL_VIEW_ASSIGNMENT);
		return;
	}
	
	//get info from database
	AssignmentCollector ac = null;
	SubmitAssignment sa = null;
	ArrayList<MarkingScheme> listMs = null;
	
	String errMsg = null;
	try {
		AssignmentsControl assCon = new AssignmentsControl();
		SubmitAssignmentControl saCon = new SubmitAssignmentControl();
		ResultControl rCon = new ResultControl();
		
		ac = assCon.getAssignment(acId);
		sa = saCon.getSubmited(acId, submitBy.getId());
		listMs = rCon.getMarkingScheme(acId);
		
		assCon.closeDBConnection();
		saCon.closeDBConnection();
		rCon.closeDBConnection();
		
	} catch(Exception err) {
		errMsg = err.toString();	
	}
	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Marking Assignment</title>
<style type="text/css">
<!--
div.MsoNormal {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}

li.MsoNormal {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}

p.MsoNormal {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}

p.MsoNormal1 {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}

p.MsoNormal11 {
	mso-style-unhide: no;
	mso-style-qformat: yes;
	mso-style-parent: "";
	margin-top: 0in;
	margin-right: 0in;
	margin-bottom: 10.0pt;
	margin-left: 0in;
	line-height: 115%;
	mso-pagination: widow-orphan;
	font-size: 11.0pt;
	font-family: "Calibri", "sans-serif";
	mso-ascii-font-family: Calibri;
	mso-ascii-theme-font: minor-latin;
	mso-fareast-font-family: ????;
	mso-fareast-theme-font: minor-fareast;
	mso-hansi-font-family: Calibri;
	mso-hansi-theme-font: minor-latin;
	mso-bidi-font-family: "Times New Roman";
	mso-bidi-theme-font: minor-bidi;
}
-->
</style>
<script type="text/javascript" src="<%= URL_JQUERY %>"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
	jQuery("input[name^='<%= PAR_MARK %>']").change(function() {
		var lbTotal = jQuery("input[name='<%= PAR_TOTAL_MARK_LB %>']");
		var mark = jQuery("input[name^='<%= PAR_MARK %>']");
		var total = 0;
		mark.each(function(){
			var aMark = jQuery(this);
			if(aMark.val() != null) {
				if(!isNaN(aMark.val())) {
					var markVal = parseFloat(aMark.val());
					var fullMark = jQuery("#fullmark_"+ aMark.attr("name").substring(5)).text();
					fullMark = parseFloat(fullMark);
					
					if(fullMark < 0 && (markVal < fullMark || markVal > 0)) {
						markVal = 0;
						aMark.val('0.00');
					} else if (fullMark >= 0 && (markVal > fullMark || markVal < 0)){
						markVal = 0;
						aMark.val('0.00');
					}
					total += markVal;
				} else {
					aMark.val('0.00');
				}
			}
		});
	lbTotal.val(parseFloat(total).toFixed(2));
	});
});
</script>
</head>

<body>
	<%
if(isEmptyString(errMsg)) {
	Hashtable<String, String> keys = new Hashtable<String, String>(2);
	keys.put(PAR_SUBMIT_BY, submitBy.getId());
	keys.put(PAR_ASSIGNMENT_ID, Integer.toString(ac.getId()));
%>
	<form action="<%= URL_CONFIRM_MARK %>" method="post">
		<input type="hidden" name="<%= PAR_ASSIGNMENT_ID%>"
			value="<%= ac.getId() %>" id="<%= PAR_ASSIGNMENT_ID%>" /> <input
			type="hidden" name="<%= PAR_SUBMIT_BY %>" value="<%= submitByStr %>"
			id="<%= PAR_SUBMIT_BY %>" /> <input type="hidden"
			id="<%= PAR_TOTAL_MARK %>" value="<%= ac.getFullMark() %>" />
		<table width="100%" border="1">
			<tr>
				<td width="40%"><strong>Assignment Name</strong>
				</td>
				<td><%= ac.getName() %></td>
			</tr>
			<tr>
				<td><strong>Student Name (ID) </strong></td>
				<td><%= submitBy.getName() %> (<%=submitBy.getId() %>)</td>
			</tr>

			<tr>
				<td><strong>Submit Time (Deadline)</strong>
				</td>
				<td><%= formatDateTime(sa.getSubmitTime()) %> (<%= formatDateTime(ac.getDeadline()) %>)</td>
			</tr>
			<tr>
				<td><strong>File</strong>
				</td>
				<td><a
					href="<%= addParmeterToUrl(URL_DOWNLOAD_ASSIGNMENT, keys) %>"><%=sa.getFileName() %></a>
				</td>
			</tr>
			<%
Iterator<MarkingScheme> lmsI = listMs.iterator();
while(lmsI.hasNext()) {
	MarkingScheme ms = lmsI.next();
%>
			<tr>
				<td rowspan="2"><p class="MsoNormal1"
						style='margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; mso-element: frame; mso-element-frame-hspace: 9.0pt; mso-element-wrap: around; mso-element-anchor-horizontal: margin; mso-element-top: 35.15pt; mso-height-rule: exactly'>
						<strong>Criteria <%= ms.getOrder() %>: <%=ms.getName() %></strong>
					</p>
					<p class="MsoNormal1"
						style='margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; mso-element: frame; mso-element-frame-hspace: 9.0pt; mso-element-wrap: around; mso-element-anchor-horizontal: margin; mso-element-top: 35.15pt; mso-height-rule: exactly'>
						<strong>Detail:</strong>
					</p>
					<p class="MsoNormal1"
						style='margin-bottom: 0in; margin-bottom: .0001pt; line-height: normal; mso-element: frame; mso-element-frame-hspace: 9.0pt; mso-element-wrap: around; mso-element-anchor-horizontal: margin; mso-element-top: 35.15pt; mso-height-rule: exactly'>
						<strong><p><%= muiliteLineString(ms.getCriteria()) %></p>
						</strong>
					</p></td>
				<td>Mark:&nbsp; <input name="<%=PAR_MARK + ms.getOrder() %>"
					type="text" id="<%=PAR_MARK + ms.getOrder() %>" maxlength="7"
					value="0.00" /> / <span id="fullmark_<%= ms.getOrder() %>"><%= ms.getMark() %></span></td>
			</tr>
			<tr>
				<td>Feedback:<br /> <textarea
						id="<%=PAR_FEEDBACK + ms.getOrder() %>"
						name="<%=PAR_FEEDBACK + ms.getOrder() %>" rows="5" cols="50">(None)</textarea>
				</td>
			</tr>
			<%}%>
			<tr>
				<td>Total Mark</td>
				<td><input name="<%= PAR_TOTAL_MARK_LB %>" type="text"
					maxlength="7" readonly="readonly" value="0.00" /> / <%= ac.getFullMark() %></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td><input name="button" type="submit" value="Submit Now!" />
				</td>
			</tr>

		</table>
	</form>
	<%
} else {
%>
	<p class="MsoNormal" style='tab-stops: 119.7pt'><%= errMsg %></p>
	<%} %>
</body>
</html>
